package com.foryou.system.modules.wx.controller;

import com.foryou.system.common.bootstrap.PageUtils;
import com.foryou.system.common.bootstrap.Query;
import com.foryou.system.common.exception.RollbackException;
import com.foryou.system.common.utils.FileUtil;
import com.foryou.system.common.utils.R;
import com.foryou.system.modules.wx.service.SubjectService;
import com.foryou.system.modules.wx.service.UserBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * Created by Lee.
 */
@RestController
@RequestMapping("/wx/subject")
public class SubjectController {

    private final SubjectService subjectService;

    public SubjectController(SubjectService subjectService) {
        this.subjectService = subjectService;
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        int total = subjectService.queryTotal(query);
        return new PageUtils(subjectService.queryList(query), total);
    }

    @RequestMapping("/remove")
    public R remove(@RequestBody Long[] ids) {
        subjectService.remove(ids);
        return R.ok();
    }

    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        subjectService.delete(ids);
        return R.ok();
    }

    @RequestMapping("/update")
    public R delete(@RequestBody Map<String,Object> params) {
        subjectService.update(params);
        return R.ok();
    }

    @RequestMapping("/add")
    public R add(@RequestBody Map<String, Object> params) {
        if (subjectService.add(params) > 0) {
            return R.ok();
        }
        return R.error();
    }

    @RequestMapping("/getInfo/{id}")
    public R getInfo(@PathVariable("id") Long id){
        return R.ok().put("data", subjectService.getSubjectInfo(id));
    }

    @RequestMapping("/checkSubjectName")
    public R checkSubjectName(String name) {
        boolean result = subjectService.queryByName(name)==0;
        return R.ok().put("valid", result);
    }

}
